package cn.wen.eeayjoke.adapter.home;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;

import cn.wen.base.adapter.recyclerview.QuickRecyclerViewHolder;
import cn.wen.base.adapter.recyclerview.QuickVAdapter;
import cn.wen.base.image.ImageLoader;
import cn.wen.base.vlayout.layout.LinearLayoutHelper;
import cn.wen.eeayjoke.R;
import cn.wen.eeayjoke.entity.bean.home.IndexTopicItems;
import cn.wen.eeayjoke.utils.ItemTypes;

/**
 * 首页行业头条
 */

public class HeadlinesAdapter extends QuickVAdapter<IndexTopicItems> {

    public HeadlinesAdapter(IndexTopicItems items) {
        List<IndexTopicItems> data = new ArrayList<>();
        data.add(items);
        setData(data);
        setLayoutHelper(new LinearLayoutHelper());
        setLayoutId(R.layout.main_headlines);
    }

    @Override
    protected void convert(QuickRecyclerViewHolder holder, IndexTopicItems item, int position) {
        // 背景
        holder.displayImage(R.id.iv_headlines_bg, item.bg_url, R.drawable.transparent);
        // 左边行业头条
        holder.displayImage(R.id.iv_headlines, item.pic_path, R.drawable.ic_headlines);
        // 右边更多
        holder.displayImage(R.id.iv_more, item.touTiaoMorePic, R.drawable.ic_headlines_more);
        // 滚动文字
        ViewFlipper viewFlipper = holder.getView(R.id.vf_headlines);
        viewFlipper.removeAllViews();
        List<IndexTopicItems> topicItems = item.indexTopicItems;
        for (IndexTopicItems topicItem : topicItems) {
            View hotView = LayoutInflater.from(viewFlipper.getContext())
                    .inflate(R.layout.item_main_headlines, null, false);
            ImageView ivHot = hotView.findViewById(R.id.iv_hot);
            TextView tvName = hotView.findViewById(R.id.tv_name);
            tvName.setText(topicItem.title + "");
            tvName.setTextColor(Color.parseColor("#333333"));
            if (!TextUtils.isEmpty(topicItem.fontCollor)) {
                tvName.setTextColor(Color.parseColor(topicItem.fontCollor));
            }
            ImageLoader.displayImage(ivHot, topicItem.pic_url, R.drawable.ic_headlines_hot);
            viewFlipper.addView(hotView);
        }
        viewFlipper.setAutoStart(true);
        viewFlipper.setFlipInterval(5000);

    }

    @Override
    public int getItemViewType(int position) {
        return ItemTypes.TYPE_HEADLINES;
    }
}
